<template>
  <a-spin :spinning="pageLoading">
    <a-form-model class="detail-form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="头像">
        <a-upload
            name="file"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :action="Util.getUploadImagePath()"
            :before-upload="beforeUpload"
            @change="handleChangeImage"
        >
          <img v-if="form.image" :src="form.image" alt="avatar" style="width:80px;height: 80px"/>
          <span v-else>
            <a-icon type="plus"/>
            <span class="ant-upload-text">
              Upload
            </span>
          </span>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="姓名">
        <a-input allowClear v-model="form.name"/>
      </a-form-model-item>
      <a-form-model-item label="手机号">
        <a-input allowClear v-model="form.mobile"/>
      </a-form-model-item>
      <a-form-model-item label="企业邮箱">
        <a-input allowClear v-model="form.email"/>
      </a-form-model-item>
      <a-form-model-item label="职位">
        <a-input allowClear v-model="form.position"/>
      </a-form-model-item>
      <a-form-model-item label="身份">
        <a-select allowClear placeholder="选择状态" v-model="form.type">
          <a-select-option :value="v.id" v-for="(v, i) in Config.Type" :key="i">{{ v.title }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="联系方式">
        <a-select allowClear placeholder="选择状态" v-model="form.mobileState">
          <a-select-option :value="v.id" v-for="(v, i) in Config.MobileState" :key="i">{{ v.title }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="允许同行电话联系" v-if="form.type==1">
        <a-select allowClear placeholder="选择状态" v-model="form.usState">
          <a-select-option :value="v.id" v-for="(v, i) in Config.usState" :key="i">{{ v.title }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="工作证明">
        <a-upload
            name="file"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :action="Util.getUploadImagePath()"
            :before-upload="beforeUpload"
            @change="handleChange"
        >
          <img v-if="form.workingImage" :src="form.workingImage" alt="avatar" style="width:80px;height: 80px"/>
          <span v-else>
            <a-icon type="plus"/>
            <span class="ant-upload-text">
              Upload
            </span>
          </span>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="设置密码">
        <a-input type="password" allowClear v-model="form.password"/>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 14, offset: 5 }">
        <a-button type="primary" @click="onSubmit"> 提交</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-spin>
</template>
<script>

import config from "../../tools/config";

function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

export default {
  data() {
    return {
      // 公用配置
      Config: {},
      pageLoading: false,
      uploadLoading: false,
      labelCol: {span: 5},
      wrapperCol: {span: 14},
      form: {
        workingImage: '',
        contractState: 1,
        email: null,
        enable: true,
        followState: 0,
        image: "",
        mobile: "",
        mobileState: 1,
        name: "",
        nickname: "",
        password: "",
        subscribeState: 0,
        supplier: null,
        supplierId: null,
        type: '',
        usState: 1
      },
    };
  },
  created() {
    this.Config = config
  },
  methods: {

    // 提交更新
    onSubmit() {
      if (this.form.type == 1) {
        this.form.usState = 0
      }
      this.Http.post("/admin/user/create", this.form).then(res => {
        if (res.code == 1) {
          this.$message.success(res.msg)
          setTimeout(() => {
            location.reload()
          }, 1500)
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 上传
    handleChange(info, fileList , event) {
      console.log(info,  fileList , event)
      if (info.file.status === 'uploading') {
        this.uploadLoading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        this.form.workingImage = info.fileList[0].response.data.path
        this.uploadLoading = false;
      }
    },

    handleChangeImage(info, fileList , event) {
      console.log(info,  fileList , event)
      if (info.file.status === 'uploading') {
        this.uploadLoading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        this.form.image = info.fileList[0].response.data.path
        this.uploadLoading = false;
      }
    },

    // 上传前检测格式和大小
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('Image must smaller than 2MB!');
      }
      return isJpgOrPng && isLt2M;
    },
  },
};
</script>
